package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;


    /*
     *
     * 添加购物车
     *
     * */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        //判断当前加入购物车中的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByShoppingCart(shoppingCart);

        //如果已经存在，只需将数量加一
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //如果不存在，需要插入一条购物车数据
            //插入前需要判断插入的是套餐还是菜品
            if (shoppingCart.getSetmealId() != null && shoppingCart.getDishId() == null) {
                //插入套餐时的操作
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);

                String name = setmeal.getName();
                shoppingCart.setName(name);

                BigDecimal price = setmeal.getPrice();
                shoppingCart.setAmount(price);

                String image = setmeal.getImage();
                shoppingCart.setImage(image);
            } else {
                //插入菜品时的操作
                Long dishId = shoppingCart.getDishId();

                Dish dish = dishMapper.selectById(dishId);

                String name = dish.getName();
                shoppingCart.setName(name);

                BigDecimal price = dish.getPrice();
                shoppingCart.setAmount(price);

                String image = dish.getImage();
                shoppingCart.setImage(image);
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }


    /*
     *
     * 查看购物车
     *
     * */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.select(userId);
        return shoppingCartList;
    }


    /*
     *
     * 清空购物车
     *
     * */
    @Override
    public void deleteAll() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userId);
    }



    /*
     *
     * 根据菜品id或者套餐id删除购物车中一个商品
     *
     * */
    @Override
    public void deleteByDishIdOrSetmealId(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //如果购物车中对应的商品数量不止一个，那么需要将其减一而不是直接删除,因此需要先查询获得对应商品的数量
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByShoppingCart(shoppingCart);

        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            Integer number = shoppingCartList.get(0).getNumber();
            if (number > 1) {
                //将商品减一
                ShoppingCart cart = shoppingCartList.get(0);
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(cart);
            } else if (number == 1) {
                //直接删除
                ShoppingCart cart = shoppingCartList.get(0);
                Long id = cart.getId();
                shoppingCartMapper.deleteByid(id);
            }
        }
    }
}
